package com.jojo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jojo.domain.ProdTagReference;
import com.jojo.domain.Sku;
import com.jojo.mapper.ProdTagReferenceMapper;
import com.jojo.mapper.SkuMapper;
import com.jojo.service.ProdTagReferenceService;
import com.jojo.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jojo.mapper.ProdMapper;
import com.jojo.domain.Prod;
import com.jojo.service.ProdService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService{

    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private ProdTagReferenceService prodTagReferenceService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProdTagReferenceMapper prodTagReferenceMapper;

    @Autowired
    private SkuMapper skuMapper;

    /**
     * 新增商品
     * 1. 先将商品加入表中，则有了id
     * 2. 处理商品与分组标签的关系   多对多的表
     * 3. 处理商品与商品sku的关系    新加入sku，prodId存的是新加入的商品Id
     * @param prod
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveProd(Prod prod) {
        //新增商品
        prod.setShopId(1L);
        prod.setSoldNum(0);
        prod.setCreateTime(new Date());
        //上架时间
        prod.setPutawayTime(new Date());
        prod.setUpdateTime(new Date());
        prod.setVersion(0);
        prod.setDeliveryMode(JSONObject.toJSONString(prod.getDeliveryModeVo()));
        int i = prodMapper.insert(prod);
        if (i > 0) {
            //处理商品与分组标签的关系
            //获取商品分组标签
            List<Long> tagIdList = prod.getTagList();
            if (CollectionUtil.isNotEmpty(tagIdList) && tagIdList.size()!=0) {
                List<ProdTagReference> list = new ArrayList<>();
                tagIdList.forEach(tagId -> {
                    //创建商品与分组标签的关系
                    ProdTagReference prodTagReference = new ProdTagReference();
                    prodTagReference.setProdId(prod.getProdId());
                    prodTagReference.setTagId(tagId);
                    prodTagReference.setCreateTime(new Date());
                    prodTagReference.setShopId(1L);
                    list.add(prodTagReference);
                });
                prodTagReferenceService.saveBatch(list);
            }
            //处理商品与商品sku的关系
            //获取商品sku对象集合
            List<Sku> skuList = prod.getSkuList();
            if (CollectionUtil.isNotEmpty(skuList) && skuList.size()!=0) {
                List<Sku> list = new ArrayList<>();
                skuList.forEach(sku -> {
                    sku.setProdId(prod.getProdId());
                    sku.setCreateTime(new Date());
                    sku.setUpdateTime(new Date());
                    sku.setVersion(0);
                    sku.setActualStocks(sku.getStocks());
                    list.add(sku);
                });
                skuService.saveBatch(list);
            }

        }
        return i > 0;
    }

    @Override
    public Prod queryProdInfoById(Long prodId) {
        //根据标识查询商品详情
        Prod prod = prodMapper.selectById(prodId);
        if (ObjectUtil.isNull(prod)) {
            return prod;
        }
        //根据商品标识查询商品与分组标签的关系
        List<ProdTagReference> prodTagReferenceList = prodTagReferenceMapper.selectList(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId, prodId)
        );
        //判断是否有值
        if (CollectionUtil.isNotEmpty(prodTagReferenceList) && prodTagReferenceList.size()!=0) {
            List<Long> tagIdList = prodTagReferenceList.stream().map(ProdTagReference::getReferenceId).collect(Collectors.toList());
            prod.setTagList(tagIdList);
        }
        //根据商品id查询商品sku对象集合
        List<Sku> skuList = skuMapper.selectList(new LambdaQueryWrapper<Sku>()
                .eq(Sku::getProdId, prodId)
        );
        prod.setSkuList(skuList);
        return prod;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyProdInfo(Prod prod) {
        //删除商品原有的与分组标签的关系
        prodTagReferenceMapper.delete(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId,prod.getProdId())
        );
        List<Long> tagIdList = prod.getTagList();
        if (CollectionUtil.isNotEmpty(tagIdList) && tagIdList.size()!=0) {
            List<ProdTagReference> list = new ArrayList<>();
            tagIdList.forEach(tagId -> {
                //创建商品与分组标签的关系
                ProdTagReference prodTagReference = new ProdTagReference();
                prodTagReference.setProdId(prod.getProdId());
                prodTagReference.setTagId(tagId);
                prodTagReference.setCreateTime(new Date());
                prodTagReference.setShopId(1L);
                list.add(prodTagReference);
            });
            prodTagReferenceService.saveBatch(list);
        }

        //批量修改商品sku对象集合
        List<Sku> skuList = prod.getSkuList();
        skuList.forEach(sku -> {
            sku.setUpdateTime(new Date());
            sku.setActualStocks(sku.getStocks());
        });
        skuService.updateBatchById(skuList);

        //修改商品对象
        prod.setUpdateTime(new Date());
        return prodMapper.updateById(prod) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeProdById(Long prodId) {
        //删除商品与分组标签的关系
        prodTagReferenceMapper.delete(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getProdId,prodId)
        );
        //根据商品id删除商品sku对象
        skuMapper.delete(new LambdaQueryWrapper<Sku>()
                .eq(Sku::getProdId,prodId)
        );
        return prodMapper.deleteById(prodId) > 0;
    }
}
